/*
 * Copyright (C) 2015 Daniel Uppenkamp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.maurersmarbles;

import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author Daniel Uppenkamp
 */
public class Board {

    /**
     * The board indexes of the home entry space for each player.
     */
    public static final int BOARD_INDEX_HOME_ENTRY[] = {54, 12, 26, 40};
    /**
     * The max board indexes of the home spaces for each player.
     */
    public static final int BOARD_INDEX_HOME_MAX[] = {59, 63, 67, 71};
    /**
     * The min board indexes of the home spaces for each player.
     */
    public static final int BOARD_INDEX_HOME_MIN[] = {56, 60, 64, 68};
    /**
     * The board indexes of the safe space for each player.
     */
    public static final int BOARD_INDEX_SAFE[] = {0, 14, 28, 42};
    /**
     * The max board indexes of the start spaces for each player.
     */
    public static final int BOARD_INDEX_START_MAX[] = {75, 79, 83, 87};
    /**
     * The min board indexes of the start spaces for each player.
     */
    public static final int BOARD_INDEX_START_MIN[] = {72, 76, 80, 84};
    /**
     * The size of the grid that makes up the spaces on the board.
     */
    public static final int GRID_SIZE = 15;
    /**
     * The number of playable spaces on the board.
     */
    public static final int NUMBER_OF_BOARD_SPACES = 56;
    /**
     * The number of home spaces on the board.
     */
    public static final int NUMBER_OF_HOME_SPACES = 4;
    /**
     * The map that is used to store the board index to grid cell mapping.
     */
    private static final Map<Integer, Point> BOARD_INDEX_TO_GRID_CELL_MAP = new TreeMap();

    /**
     * Returns the grid cell for the given board index.
     *
     * @param boardIndex the board index to get the grid cell for
     * @return the grid cell for the given board index
     */
    public static Point getGridCellForBoardIndex(int boardIndex) {
        return BOARD_INDEX_TO_GRID_CELL_MAP.get(boardIndex);
    }

    /**
     * Returns the index for the given cell.
     *
     * @param point the cell to get the index for
     * @return the index for the given cell
     */
    public static int getIndexForGridCell(Point point) {
        for (Map.Entry<Integer, Point> entry : BOARD_INDEX_TO_GRID_CELL_MAP.entrySet()) {
            if (entry.getValue().equals(point)) {
                return entry.getKey();
            }
        }
        return -1;
    }

    static {

        //    | 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14
        // ---|---------------------------------------------
        // 00 |                24 25 26 27 28
        // 01 |       76 77    23    64    29    80 81
        // 02 |                22    65    30
        // 03 |       78 79    21    66    31    82 83
        // 04 |                20    67    32
        // 05 | 14 15 16 17 18 19          33 34 35 36 37 38
        // 06 | 13                                        39
        // 07 | 12 60 61 62 63                71 70 69 68 40
        // 08 | 11                                        41
        // 09 | 10 09 08 07 06 05          47 46 45 44 43 42
        // 10 |                04    59    48
        // 11 |       72 73    03    58    49    84 85
        // 12 |                02    57    50
        // 13 |       74 75    01    56    51    86 87
        // 14 |                00 55 54 53 52
        //
        // playable spaces
        BOARD_INDEX_TO_GRID_CELL_MAP.put(0, new Point(5, 14));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(1, new Point(5, 13));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(2, new Point(5, 12));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(3, new Point(5, 11));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(4, new Point(5, 10));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(5, new Point(5, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(6, new Point(4, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(7, new Point(3, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(8, new Point(2, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(9, new Point(1, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(10, new Point(0, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(11, new Point(0, 8));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(12, new Point(0, 7));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(13, new Point(0, 6));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(14, new Point(0, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(15, new Point(1, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(16, new Point(2, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(17, new Point(3, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(18, new Point(4, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(19, new Point(5, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(20, new Point(5, 4));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(21, new Point(5, 3));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(22, new Point(5, 2));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(23, new Point(5, 1));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(24, new Point(5, 0));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(25, new Point(6, 0));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(26, new Point(7, 0));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(27, new Point(8, 0));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(28, new Point(9, 0));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(29, new Point(9, 1));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(30, new Point(9, 2));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(31, new Point(9, 3));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(32, new Point(9, 4));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(33, new Point(9, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(34, new Point(10, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(35, new Point(11, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(36, new Point(12, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(37, new Point(13, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(38, new Point(14, 5));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(39, new Point(14, 6));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(40, new Point(14, 7));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(41, new Point(14, 8));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(42, new Point(14, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(43, new Point(13, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(44, new Point(12, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(45, new Point(11, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(46, new Point(10, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(47, new Point(9, 9));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(48, new Point(9, 10));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(49, new Point(9, 11));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(50, new Point(9, 12));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(51, new Point(9, 13));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(52, new Point(9, 14));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(53, new Point(8, 14));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(54, new Point(7, 14));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(55, new Point(6, 14));
        // home spaces
        BOARD_INDEX_TO_GRID_CELL_MAP.put(56, new Point(7, 13));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(57, new Point(7, 12));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(58, new Point(7, 11));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(59, new Point(7, 10));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(60, new Point(1, 7));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(61, new Point(2, 7));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(62, new Point(3, 7));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(63, new Point(4, 7));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(64, new Point(7, 1));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(65, new Point(7, 2));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(66, new Point(7, 3));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(67, new Point(7, 4));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(68, new Point(13, 7));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(69, new Point(12, 7));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(70, new Point(11, 7));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(71, new Point(10, 7));
        // start spaces
        BOARD_INDEX_TO_GRID_CELL_MAP.put(72, new Point(1, 11));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(73, new Point(3, 11));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(74, new Point(1, 13));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(75, new Point(3, 13));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(76, new Point(1, 1));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(77, new Point(3, 1));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(78, new Point(1, 3));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(79, new Point(3, 3));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(80, new Point(11, 1));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(81, new Point(13, 1));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(82, new Point(11, 3));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(83, new Point(13, 3));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(84, new Point(11, 11));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(85, new Point(13, 11));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(86, new Point(11, 13));
        BOARD_INDEX_TO_GRID_CELL_MAP.put(87, new Point(13, 13));

    }

}
